package com.example.myapplication.ui.activity.setting

import androidx.lifecycle.Observer
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.myapplication.R
import com.example.myapplication.arouter.ARouterPath
import com.example.myapplication.ui.vm.SharedViewModel
import com.kun.base.env.Constants
import com.kun.base.ui.AbsBaseActivity
import com.kun.base.util.MMKVUtils
import kotlinx.android.synthetic.main.activity_setting.*
import kotlinx.android.synthetic.main.toolbar_layout.*

/**
 *设置的activity
 *
 * @author : Kun
 * @date : 2021/6/7
 * @time : 14:35
 */
@Route(path = ARouterPath.SETTING)
class SettingActivity : AbsBaseActivity() {
    private lateinit var settingViewModel: SettingViewModel
    private lateinit var sharedViewModel: SharedViewModel

    override fun getLayoutId(): Int {
        return R.layout.activity_setting
    }

    override fun initViewModel() {
        settingViewModel = getActivityScopeViewModel(SettingViewModel::class.java)
        sharedViewModel = getApplicationScopeViewModel(SharedViewModel::class.java)
        settingViewModel.loginRequest.logoutResult.observe(this, Observer {
            if (it.errorCode == 0) {
                MMKVUtils.remove(Constants.USERNAME_CACHE_KEY)
                sharedViewModel.userdata.value = null
                finish()
            }
        })
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        tv_title.text = getString(R.string.setting)
        tv_logout.setOnClickListener {
            settingViewModel.requestLogout()
        }
    }
}